﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;
using System.Web;
using System.Web.UI.HtmlControls;
using ZCZP.Web.Site;

namespace BySoft.Web.Site.OutSource
{
    public partial class OutBackMatEdit : AuthBasePage
    {
        #region 属性定义
        private string m_strsequenceName = "BsOutbackmat" + DateTime.Now.ToString("yyyyMMdd");
        private string m_strTbl = "BS_OutBackMat";
        private string m_strMainKey = "BillID";

        private DataTable DtOutBackMatData
        {
            get
            {
                if (null == Session["OutBackMatData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["OutBackMatData"];
                }
            }
            set
            {
                Session["OutBackMatData"] = value;
            }
        }

        private DataTable CreateDT()
        {

            return BsOutbackmat.CreateDT();
        }
        #endregion

        #region 初始化及绑定控件
        private void InitData()
        {
            DtOutBackMatData = null;
            SetControlAttr(new BsOutbackmatEntity(), tBase);
            string strCode = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            InitHead(strCode);
            InitBody(strCode);
        }

        private void InitHead(string strCode)
        {
            //DataTable dt = BsOutbackmat.SelectHeadData(strCode);
            //DBtoControlHead(this.Page, dt, tBase, @"../OutOrder/OutBackMatList.aspx");
            //DataRow row = dt.Rows[0];
            //hidOutOrderID.Value = CommonMethod.GetString(row["OutOrderID"]);
            //hdCode.Value = CommonMethod.GetString(row["BillID"]);
            
        }

        private void InitBody(string strCode)
        {
            DataTable dt = BsOutbackmat.SelectHeadData(strCode);
            if (dt.Rows.Count <=0)
            {
                return;
            }
            DataRow dr = dt.Rows[0];

            hdCode.Value = strCode;
            lblBillID.Text = strCode;
            lblBillDate.Text = CommonMethod.GetString(dr["BillDate"]);
            hidOutOrderID.Value = CommonMethod.GetString(dr["OutOrderID"]);

               string sendID=hidOutOrderID.Value;
                string materialID = dr["MaterialID"].ToString();
                DataTable dtCanBack = BsOutorder.SelectMatCanBackNum(strCode,sendID,materialID);
                if (dtCanBack.Rows.Count > 0)
                {

                DataRow dr0 = dtCanBack.Rows[0];
             
                dr["CanBackNum"] = dr0["CanBackNum"];
                }
                else
                {
                  
                    dr["CanBackNum"] = dr["Nums"];
                }


                txtNums.Text = CommonMethod.GetString(dr["Nums"]);

                FillHead( dr);

            DtOutBackMatData = dt.Copy();
            BindData();
        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                InitData();
            }
            Master.ValidationGroup = "gSubmit";
                 }



       

        private void BindData()
        {

            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }



        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
            {
                return;
            }

            BsOutbackmatEntity entity = new BsOutbackmatEntity();
            entity.OutOrderID = GetString(txtOutOrderID);

            entity.WorkOrderNo = GetString(txtWorkOrderNo);
            entity.SupplierID = GetString(hidSupplierID);
            entity.MaterialID = GetString(txtMaterialID);
            entity.Nums = GetDecimal(txtNums);

            entity.BillDate = GetDateTime(lblBillDate);
            entity.BillID = GetString(lblBillID);
            entity.IsRepair = CommonMethod.GetString(hidIsRepair.Value); 

            entity.Status = "1";
            entity.ModifyUserID = CurrentUser.UserID;
            entity.ModifyUserName = CurrentUser.UserName;
            entity.ModifyDate = DateTime.Now;





            BsOutbackmat.SaveOrUpdate(entity, false);
            base.LogAction(BaseEnum.ActionType.Add, entity.BillID, true, null, new EntityBase[] { entity });
            DtOutBackMatData = null;
            UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../OutOrder/OutBackMatList.aspx");
        }

     
        protected void btnSelectOutOrderID_Click(object sender, EventArgs e)
        { 
            BindDetailData();

        }

        private void BindDetailData()
        {
            string strOutOrderID = hidOutOrderID.Value.Trim();

            if (string.IsNullOrEmpty(strOutOrderID))
            {
                return;
            }
            DataTable dtTemp = CreateDT();


            bool isRepair = CommonMethod.GetString(hidIsRepair.Value).Equals("1");

            DataTable dt = BsOutbackmat.SelectOutProcessDetail(isRepair, lblBillID.Text, strOutOrderID);
            if (dt.Rows.Count != 0)
            {

                DataRow dr = dt.Rows[0];     
                txtNums.Text = CommonMethod.GetString(dr["CanBackNum"]);

                FillHead( dr);
            }   
            BindData();
         
        }

        private void FillHead( DataRow dr)
        {
            txtOutOrderID.Text = CommonMethod.GetString(dr["OutOrderID"]);
            txtWorkOrderNo.Text = CommonMethod.GetString(dr["WorkOrderNo"]);
            txtScheduleID.Text = CommonMethod.GetString(dr["ScheduleID"]);
            hidSupplierID.Value = CommonMethod.GetString(dr["SupplierID"]);
            txtSupplierName.Text = CommonMethod.GetString(dr["SupplierName"]);
          //  txtOutOrderID.Text = strOutOrderID;
            txtBackProcessID.Text = CommonMethod.GetString(dr["BackProcessID"]);
            txtMaterialID.Text = CommonMethod.GetString(dr["BackMaterialID"]);

            txtOutProcessID.Text = CommonMethod.GetString(dr["OutProcessID"]);
            txtMaterialName.Text = CommonMethod.GetString(dr["BackMaterialName"]);
            txtSpecifications.Text = CommonMethod.GetString(dr["Specifications"]);
            txtUnit.Text = CommonMethod.GetString(dr["Unit"]);
            txtCanBackNum.Text = CommonMethod.GetString(dr["CanBackNum"]);

            hidIsRepair.Value = CommonMethod.GetString(dr["IsRepair"]);
        }


        #endregion



        #region 本地方法
        private bool ValidateData()
        {
            string strMention = "";


            //if (ClsComm.CheckExist(m_strTbl, m_strMainKey, lblBillID.Text.Trim(), true))
            //{
            //    strMention = "委外件退料编号已经存在！<br />";
            //}

            decimal sum = CommonMethod.ConvertToDecimal(txtNums.Text, 2);

            if (sum <= 0)
            {
                strMention = "回厂数量不可为空！<br />";
            }
            if (string.IsNullOrEmpty(strMention))
            {
                return true;
            }
            else
            {
                UIHelper.AlertMessage(this.Page, strMention);
                return false;
            }
        }
        #endregion
    }
}

